package com.mazong.serversecaa;

import com.mazong.serversecaa.pojo.TbRole;
import com.mazong.serversecaa.pojo.TbRolePermission;
import com.mazong.serversecaa.pojo.TbUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component(value = "iDbSecurityMapper")
public interface IDbSecurityMapper {

    //-- 获取用户信息
    @Select("SELECT id,username,password FROM user WHERE username = #{username}")
    TbUser getUserByName(@Param("username") String username);

    //-- 根据用户ID获取拥有的角色
    @Select("SELECT id,name FROM role WHERE id IN (SELECT role_id FROM user_role WHERE user_id=${userId})")
    List<TbRole> getRoleByUserId(@Param("userId") Integer userId);

    //-- 获取所有角色对应权限
    @Select("SELECT B.name AS roleName,C.url FROM role_permission AS A " +
                    "LEFT JOIN role AS B ON A.role_id=B.id " +
                    "LEFT JOIN permission AS C ON A.permission_id=C.id ")
    List<TbRolePermission> getRolePermissons();

}
